{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Vector store-backed retriever\n",
    "A vector store retriever is a retriever that uses a vector store to retrieve documents. It is a lightweight wrapper around the vector store class to make it conform to the retriever interface. It uses the search methods implemented by a vector store, like similarity search and MMR, to query the texts in the vector store.\n",
    "\n",
    "向量存储检索器是使用向量存储来检索文档的检索器。它是向量存储类的轻量级包装器，以使其符合检索器接口。它使用向量存储实现的搜索方法（如相似性搜索和 MMR）来查询向量存储中的文本。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.document_loaders import TextLoader\n",
    "from langchain_community.vectorstores import FAISS\n",
    "from langchain_openai import OpenAIEmbeddings\n",
    "from langchain_text_splitters import CharacterTextSplitter\n",
    "\n",
    "\n",
    "loader = TextLoader(\"../../state_of_the_union.txt\")\n",
    "\n",
    "documents = loader.load()\n",
    "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
    "texts = text_splitter.split_documents(documents)\n",
    "embeddings = OpenAIEmbeddings()\n",
    "db = FAISS.from_documents(texts, embeddings)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Similarity search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "retriever = db.as_retriever()\n",
    "docs = retriever.invoke(\"what did he say about ketanji brown jackson\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Maximum marginal relevance retrieval\n",
    "最大边际相关性检索\n",
    "\n",
    "By default, the vector store retriever uses similarity search. If the underlying vector store supports maximum marginal relevance search, you can specify that as the search type.\n",
    "\n",
    "默认情况下，向量存储检索器使用相似性搜索。如果基础向量存储支持最大边际相关性搜索，则可以将其指定为搜索类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "retriever = db.as_retriever(search_type=\"mmr\")\n",
    "\n",
    "docs = retriever.invoke(\"what did he say about ketanji brown jackson\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Similarity score threshold retrieval\n",
    "相似性分数阈值检索\n",
    "\n",
    "You can also set a retrieval method that sets a similarity score threshold and only returns documents with a score above that threshold.\n",
    "\n",
    "您还可以设置一种检索方法，该方法设置相似性分数阈值，并且仅返回分数高于该阈值的文档。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "retriever = db.as_retriever(\n",
    "    search_type=\"similarity_score_threshold\", search_kwargs={\"score_threshold\": 0.5}\n",
    ")\n",
    "docs = retriever.invoke(\"what did he say about ketanji brown jackson\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Specifying top k \n",
    "指定前 k\n",
    "\n",
    "You can also specify search kwargs like k to use when doing retrieval.\n",
    "\n",
    "您还可以指定在进行检索时要 k 使用的搜索 kwargs。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "retriever = db.as_retriever(search_kwargs={\"k\": 1})\n",
    "docs = retriever.invoke(\"what did he say about ketanji brown jackson\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain0_1",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
